package com.haiertvbic.tvnow.adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.common.Constant;
import com.haiertvbic.tvnow.holder.LogoHolder;
import com.haiertvbic.tvnow.model.ChannelLogoBean;
import com.haiertvbic.tvnow.network.EPGWeb;
import com.haiertvbic.tvnow.util.SLog;

/**
 * 台标的adpter
 * 
 * @author sun
 * 
 */
public class ChannelLogoAdapter extends BaseAdapter {
	private static final String TAG = null;
	private ArrayList<ChannelLogoBean> logos;
	private Context context;
	private int sreen_height;
	private Map<String, SoftReference<Bitmap>> logosCaches = new HashMap<String, SoftReference<Bitmap>>();

	/**
	 * @param logos
	 *            获取到的五个常看频道
	 * @param context
	 * @param sreen_height
	 *            屏幕的高度，用于计算可以显示的图片
	 */
	public ChannelLogoAdapter(ArrayList<ChannelLogoBean> logos,
			Context context, int sreen_height) {
		// TODO Auto-generated constructor stub
		this.logos = logos;
		this.context = context;
		this.sreen_height = sreen_height;
		this.logosCaches = Constant.logosCaches;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		SLog.d(TAG, "========getCount============");
		return logos.size();
	}

	@Override
	public Object getItem(int position) {
		SLog.d(TAG, "========getItem============");
		// TODO Auto-generated method stub
		return logos.get(position);
	}

	@Override
	public long getItemId(int position) {
		SLog.d(TAG, "========getItemId============");
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SLog.d(TAG, "========getView============");
		final LogoHolder holder = new LogoHolder();
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.chann_logo_item, null);
		}
		holder.chan_item_ll = (LinearLayout) convertView
				.findViewById(R.id.chan_item_ll);
		android.view.ViewGroup.LayoutParams ll_lp = holder.chan_item_ll
				.getLayoutParams();
		ll_lp.height =( sreen_height -Constant.RANGE_TOP)/ Constant.CHANNEL_NUMBER;
		holder.chan_item_ll.setLayoutParams(ll_lp);
		// =========动态控制图片张数=================
		holder.logo_iv = (ImageView) convertView.findViewById(R.id.logo_iv);

		// ============寻找控件完毕====================
		String urlStr = logos.get(position).getLogourl();
		if (urlStr != null && !"http://tvsou.comNULL".equals(urlStr)) {
			if (logosCaches.containsKey(urlStr)
					&& (logosCaches.get(urlStr).get() != null)) {
				// 若软引用中有，给image设置
				holder.logo_iv.setImageBitmap(logosCaches.get(urlStr).get());
				SLog.d(TAG, "软引用的台标");
			} else {
				// 开异步线程去台标
				SLog.d(TAG, "开异步线程取的台标");
				new LogoAsyncTask(logos, holder, position).execute();
			}
		}
		return convertView;
	}

	/**
	 * 开异步线程获取台标
	 * 
	 * @author sun
	 * 
	 */
	class LogoAsyncTask extends AsyncTask<Void, Void, Bitmap> {

		private LogoHolder logo_holder;
		private ArrayList<ChannelLogoBean> logos;
		private int postion;

		public LogoAsyncTask(ArrayList<ChannelLogoBean> logos,
				LogoHolder logo_holder, int postion) {
			// TODO Auto-generated constructor stub
			this.logos = logos;
			this.logo_holder = logo_holder;
			this.postion = postion;
		}

		@Override
		protected void onPreExecute() {
			logo_holder.logo_iv.setImageResource(R.drawable.tv_now);
			super.onPreExecute();
		}

		@Override
		protected Bitmap doInBackground(Void... params) {
			Bitmap bm = null;
			if (!logos.get(postion).getLogourl().equals("http://tvsou.comNULL")) {
				String urlStr = logos.get(postion).getLogourl();
				bm = EPGWeb.getBitmapFromURL(context, urlStr);
			}
			SLog.d(TAG, "====doInBackground============获取到台标了");
			return bm;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			logo_holder.logo_iv.setImageBitmap(result);
			SLog.d(TAG, "=======onPostExecute================获取到台标了");
			logosCaches.put(logos.get(postion).getLogourl(),
					new SoftReference<Bitmap>(result));
			super.onPostExecute(result);
		}
	}
}
